args <- commandArgs(trailingOnly=TRUE)
setwd(args[1])
library(limma)
data<-read.table("rma.txt",sep="\t",head=T)
controls<-read.table("control")
c<-as.vector(as.character(controls[,1]))
senescence<-read.table("senescence")
t<-as.vector(as.character(senescence[,1]))
rownames(data)<-data[,1]
data<-data[,2:ncol(data)]
data<-data[,c(c,t)]
print(dim(data))
print(length(c))
print(length(t))
data<-log2(data)
groups<-c(rep("C",length(c)),rep("T",length(t)))
groups<-as.factor(groups) 
design<-model.matrix(~groups)
fit <- lmFit(data, design)
fit <- eBayes(fit)
tt<-toptable(fit, coef=2, n=nrow(data))
selected.rows<-rownames(data[as.numeric(rownames(tt)),])
descript<-data[selected.rows,]
fc<-(2**abs(tt[,"logFC"]))*(tt[,"logFC"]/abs(tt[,"logFC"]))
descript <-as.matrix(descript)
tt <-as.matrix(tt)
result<-cbind(selected.rows,descript,fc,tt)
rownames(result)<-tolower(result[,1])
write.table(result,"Diff_exp.txt",sep="\t",col.names=T,row.names=F,quote=F)
#reading the negative id.s
ids<-read.table("/data4/bsi/bioinf_ext1/s113890.senescence/Potential_Negative_converted_ids_probe.txt",sep="\t",head=T)
rownames(ids)<-tolower(ids[,2])
nega <-cbind(ids[intersect(as.vector(tolower(ids[,2])), as.vector(tolower(rownames(result)))),],result[intersect(as.vector(tolower(ids[,2])), as.vector(tolower(rownames(result)))),])

#reading the positiveid.s
ids<-read.table("/data4/bsi/bioinf_ext1/s113890.senescence/Potential_Positive_converted_ids_probe.txt",sep="\t",head=T)
rownames(ids)<-tolower(ids[,2])
posi <- cbind(ids[intersect(as.vector(tolower(ids[,2])), as.vector(tolower(rownames(result)))),],result[intersect(as.vector(tolower(ids[,2])), as.vector(tolower(rownames(result)))),])
write.table(nega, "Diff_exp_negative.xls", sep="\t", col.names=T,row.names=F,quote=F)
write.table(posi, "Diff_exp_positive.xls", sep="\t", col.names=T,row.names=F,quote=F)
